package model;

import java.util.ArrayList;
import java.util.List;

import domain.DomainException;
import domain.iObserver;

public class TicTacToeGame implements Game{
	private Board board;
	private char[] signs = {'O', 'X'};
	private String titel;
	private int currentSign = 0;
	private List<iObserver> observers = new ArrayList<iObserver>();

	
	public TicTacToeGame(){
		board = new Board(3, 3);
		setTitel("TicTacToe");
	}
	
	@Override
	public void play(int row, int col) throws DomainException{
		//check if valid move
		board.set(row, col, getSign());

		//check if game over
		moveToNextSign();
		notifyObservers();
	}

	private char getSign() {
		return signs[currentSign];
	}

	private void moveToNextSign() {
		currentSign ++;
		currentSign = currentSign % 2;
	}
	
	@Override
	public Board getBoard(){
		return board;
	}
	
	private void setTitel(String titel){
		this.titel = titel;
	}
	
	@Override
	public String getTitel() {
		return titel;
	}

	@Override
	public void notifyObservers() {
		for(iObserver o: observers){
	         o.update(this);
	      }		
	}

	@Override
	public void registerObserver(iObserver obs) {
		observers.add(obs);
	}

	@Override
	public void removeObserver(iObserver obs) {
		observers.remove(obs);
	}

	@Override
	public void voegToeAanTitel(String naam) {
		if (getTitel().contains(" ")){
			setTitel(getTitel() + "/" + naam);
		}else{
			setTitel(getTitel() + " " + naam);
		}
		
	}

}